package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.common.BizException;
import com.yuelao.yuelao_backend.common.ErrorCode;
import com.yuelao.yuelao_backend.dto.CommentDTO;
import com.yuelao.yuelao_backend.dto.PostDTO;
import com.yuelao.yuelao_backend.dto.request.AddCommentRequest;
import com.yuelao.yuelao_backend.dto.request.AddPostRequest;
import com.yuelao.yuelao_backend.service.PostService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid; // 使用 jakarta.validation

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/posts")
@Slf4j
public class PostController {

    @Resource
    private PostService postService;

    /**
     * 获取动态列表
     * GET /posts/{type}
     * @param type 动态类型，可选值：recommend(推荐)、follow(关注)、nearby(附近)
     * @param page 页码，默认1
     * @param size 每页数量，默认10
     * @param latitude 纬度 (仅当type为nearby时需要)
     * @param longitude 经度 (仅当type为nearby时需要)
     * @return 动态列表
     */
    @GetMapping("/{type}")
    public ApiResponse<List<PostDTO>> getPosts(@PathVariable String type,
                                               @RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "10") int size,
                                               @RequestParam(required = false) BigDecimal latitude,
                                               @RequestParam(required = false) BigDecimal longitude) {
        if (!"recommend".equalsIgnoreCase(type) && !"follow".equalsIgnoreCase(type) && !"nearby".equalsIgnoreCase(type)) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "不支持的动态类型: " + type);
        }
        List<PostDTO> posts = postService.getPosts(type, page, size, latitude, longitude);
        return ApiResponse.ok(posts);
    }

    /**
     * 获取动态评论
     * GET /posts/{postId}/comments
     * @param postId 动态ID
     * @return 评论列表
     */
    @GetMapping("/{postId}/comments")
    public ApiResponse<List<CommentDTO>> getComments(@PathVariable Long postId) {
        if (postId == null || postId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "动态ID无效");
        }
        List<CommentDTO> comments = postService.getComments(postId);
        return ApiResponse.ok(comments);
    }

    /**
     * 点赞动态
     * POST /posts/{postId}/like
     * @param postId 动态ID
     * @return 是否成功
     */
    @GetMapping("/{postId}/like")
    public ApiResponse<Boolean> likePost(@PathVariable Long postId) {
        if (postId == null || postId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "动态ID无效");
        }
        Long userId = UserContextUtil.getCurrentUserId();
        boolean result = postService.likePost(postId, userId);
        return ApiResponse.ok(result);
    }

    /**
     * 评论动态
     * POST /posts/{postId}/comment
     * @param postId 动态ID
     * @param request 评论请求体 (包含 content, parentCommentId, targetUserId)
     * @return 是否成功
     */
    @PostMapping("/{postId}/comment")
    public ApiResponse<Boolean> commentPost(@PathVariable Long postId, @Valid @RequestBody AddCommentRequest request) {
        if (postId == null || postId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "动态ID无效");
        }
        Long currentUserId = UserContextUtil.getCurrentUserId();
        boolean result = postService.commentPost(postId, currentUserId, request);
        return ApiResponse.ok(result);
    }

    /**
     * 发布动态
     * POST /posts
     * @param request 动态请求体
     * @return 是否成功
     */
    @PostMapping
    public ApiResponse<Boolean> addPost(@Valid @RequestBody AddPostRequest request) {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        boolean result = postService.addPost(currentUserId, request);
        return ApiResponse.ok(result);
    }
}